<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="80px">
      <el-form-item label="月份" prop="month">
        <el-date-picker
          v-model="queryParams.month"
          value-format="YYYY-MM"
          type="month"
          format="YYYY-MM"
          placeholder="请选择月份"
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工姓名"
          clearable
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNumber">
        <el-input
          v-model="queryParams.jobNumber"
          placeholder="请输入工号"
          clearable
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model.number="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button
          type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['oa:attendance-detail:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="summarizeDetailList" :stripe="true" :show-overflow-tooltip="true" :fit="true">
      <el-table-column label="工号" align="center" prop="jobNumber" width="100" />
      <el-table-column label="姓名" align="center" prop="userName" min-width="100" />
      <el-table-column label="部门" align="center" prop="deptName" width="100" />
      <el-table-column label="月份" align="center" prop="month" width="90" />
      <el-table-column label="应出勤(天)" align="center" prop="shouldAttend" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="实际出勤(天)" align="center" prop="actualAttend" :formatter="formatDecimal" min-width="110" />
      <el-table-column label="年假(天)" align="center" prop="annualLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="调休(小时)" align="center" prop="adjustLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="事假(天)" align="center" prop="personalLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="病假(天)" align="center" prop="sickLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="丧假(天)" align="center" prop="bereavementLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="产假(天)" align="center" prop="maternityLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="产检假(天)" align="center" prop="prenatalLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="婚假(天)" align="center" prop="marriageLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="育儿假(天)" align="center" prop="parentalLeave" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="出差(小时)" align="center" prop="travel" :formatter="formatDecimal" min-width="100" />
      <el-table-column label="加班(小时)" align="center" prop="overtime" :formatter="formatDecimal" min-width="100" />
      <el-table-column v-for="i in 31" :key="i" :label="`${i}号`" :prop="`day${i.toString().padStart(2, '0')}`" align="center" min-width="100" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
</template>

<script setup lang="ts">
import { AttendanceDetailApi } from '@/api/oa/attendancedetail'
import download from '@/utils/download'

/** 员工考勤月报表 列表 */
defineOptions({ name: 'AttendanceDetailSummarize' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const now = new Date()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  month: `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`,
  name: undefined,
  jobNumber: undefined,
  deptId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const summarizeDetailList = ref<any[]>([])

/** 格式化小数显示 */
const formatDecimal = (row: any, column: any, cellValue: any) => {
  if (cellValue === null || cellValue === undefined) return '0.00'
  const num = Number(cellValue)
  return num.toFixed(2)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  // 重置月份为本月
  queryParams.month = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    // 调用后端 /calendar-export-excel 导出接口
    const data = await AttendanceDetailApi.exportAttendanceCalendar(queryParams)
    download.excel(data, '员工考勤月报表.xls')
    message.success('导出成功')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

// 获取考勤数据
const getList = async () => {
  loading.value = true
  try {
    const data = await AttendanceDetailApi.getAttendanceCalendarPage(queryParams)
    summarizeDetailList.value = data.list
    console.log(summarizeDetailList.value)
    total.value = data.total
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getList()
})
</script>